// 引用pool.mjs
import client from '../../utils/pool.mjs'
import brandJson from './carConfigListExt.json' assert { type: "json" };

const carJsonList = brandJson.data;
const typeMapReverse = {
    '汽油': 1,
    '纯电': 2,
    '纯电动': 2,
    '插电式混合动力': 3,
    '增程式': 4,
    '油电混合': 5,
    '汽油+48V轻混系统': 6,
    '汽油+24V轻混系统': 7,
    '氢燃料': 8
}
async function start() {
    // 循环brands，如果brands[].name === carJsonList[].name，则把carJsonList[].id赋值给brands[].qiche_id字段
    for (let i = 0; i < carJsonList.length; i++) {
            const brand = await client.brand.findFirst({
                where: {
                    qiche_id: carJsonList[i].brand_id
                }
            })
            const car = await client.car.findFirst({
                where: {
                    qiche_car_id: carJsonList[i].model_id
                }
            })
            if (brand && car) {
                const jsonData = carJsonList[i]
                const data = {
                    car_id: car.id,
                    year: jsonData.decade || null,
                    qiche_config_id: jsonData.car_id,
                    factory_id: jsonData.ext.factoryId,
                    factory_name: jsonData.ext.factoryName,
                    brand_name: jsonData.ext.brandName,
                    brand_id: brand.id,
                    origin: jsonData.ext.origin,
                    name: jsonData.ext.car_name,
                    price: jsonData.ext.market_price,
                    discount: jsonData.ext.discount.toString(),
                    listing_date: jsonData.batchId,
                    energy_type: typeMapReverse[jsonData.ext.fueltype],
                    insurance: jsonData.ext.calculat.basic.xinchebaoxian.toString(),
                    ext: JSON.stringify(jsonData.ext)
                }
                await client.car_config.create({
                    data
                })
            }

    }

}
start()
